package com.tiankun.eurekaadmin.controller;

import com.tiankun.eurekaadmin.exception.CustomerException;
import com.tiankun.eurekaadmin.util.JsonResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartException;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author 伍炳清
 * @date 2020-10-20 11:17
 */
@RestController
@ControllerAdvice
public class ExceptionController {

    JsonResult jsonResult = new JsonResult();

    @ExceptionHandler(MultipartException.class)
    public Map uploadExcepttion(MultipartException e) {

        jsonResult.setCode("0");
        jsonResult.setMsg("文件不能超过1M");
        return jsonResult.getValues();
    }

    @ExceptionHandler(Throwable.class)
    public Map hander(Exception e) {

        if (e instanceof CustomerException) {
            jsonResult.setCode("0");
            jsonResult.setMsg(e.getMessage());

        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("系统走丢了！");
            //springboot 开发程序，打包成jar包，在服务器中运行
            //挂起:nohup java -jar ***.jar &

            //异常信息记录到nohup.out文件中
            SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日HH小时mm分钟ss秒");
            System.out.println("---------------" + format.format(new Date()) + "----------------");

//            System.out.println(e.toString());
            // 如果项目上线，可以不打印栈信息
            e.printStackTrace();
        }

        return jsonResult.getValues();
    }


}
